package com.ruksis.motogeek;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ToggleButton;

public class MotoGeekActivity extends Activity {
    /** Called when the activity is first created. */
	
	private View _mainView;
	private MotoServiceConnection _serviceConn;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Intent _serviceIntent = new Intent(this, MotoService.class);
        startService(_serviceIntent);
        _serviceConn = new MotoServiceConnection(new IncomingHandler());
        bindService(_serviceIntent, _serviceConn, 0);

        _mainView = findViewById(R.layout.main);
        ToggleButton _startStop = (ToggleButton) _mainView.findViewById(R.id.toggleStart);
        _startStop.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if ( isChecked ) {
					_serviceConn.startDataCollection();
				} else {
					_serviceConn.stopDataCollection();
				}
			}
		});
        
    }
    
    private class IncomingHandler extends Handler {
    	
    	@Override
        public void handleMessage(Message msg) {
    		
    		if ( msg.what == MotoMessage.GFORCE ) {
    			EditText _gf = (EditText) _mainView.findViewById(R.id.gforce);
    			_gf.setText(((Float) msg.obj).toString());
    		}
    		
        }
    	
    }
    
}